package com.star.mall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.star.mall.base.query.Query;
import com.star.mall.persistence.entity.FileRecord;
import com.star.mall.persistence.service.IFileRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 文件服务
 * @author StarQ
 * @since 2021-05-04
 */
@RestController
@RequestMapping("/api/v1/file")
@Api(value = "文件服务", tags = {"文件服务"})
public class FileController {

    @Resource
    IFileRecordService fileRecordService;

    @PostMapping("")
    @ApiOperation(value = "上传文件", notes = "上传文件", httpMethod = "POST")
    public FileRecord uploadFile(@RequestBody MultipartFile file) {
        return fileRecordService.uploadFile(file);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "下载文件", notes = "下载文件", httpMethod = "GET")
    public void downloadFile(@PathVariable String id, HttpServletResponse response) {
        fileRecordService.downloadFile(id, response);
    }

    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除文件", notes = "删除文件", httpMethod = "DELETE")
    public void deleteFile(@PathVariable String ...ids) {
        fileRecordService.deleteFile(ids);
    }

    @PostMapping("/page")
    @ApiOperation(value = "文件列表分页", notes = "文件列表分页", httpMethod = "POST")
    public Page<FileRecord> query(@RequestBody Query<FileRecord> query) {
        return fileRecordService.page(query.getPage(), query.toWrapper(FileRecord.class));
    }

}
